package com.odais.Reservation;

import java.util.Calendar;

import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.shape.Shape;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.adt.align.HorizontalAlign;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

import com.odais.AE_Modifier.BoxDropper;
import com.odais.AE_Planner.RoomObject;
import com.odais.AE_Planner.SceneContext;

/**
 *
 * @author Lionel DECELLE
 * @since 12 juin 2012 - 16:52:53
 *
 */
public class ReservationBox extends RoomObject
{
// ===========================================================
// Constants
// ===========================================================
	static final int RESERVATION_BOX_WIDTH = 150;
	static final int RESERVATION_BOX_HEIGHT = 80;

// ===========================================================
// Fields
// ===========================================================
	SceneContext sceneContext;
	private float width, height, pX, pY;
	Rectangle rectangle;

	private BitmapTextureAtlas mFontTexture;
	private Font mFont;
	private Text textName, textNPeople, textHour, textDate;
	
	private BoxDropper boxDropper;
	
	private Reservation parentReservation;
	
	public boolean isGrabbed = false;

// ===========================================================
// Constructors
// ===========================================================
	public ReservationBox(SceneContext sceneContext, float pX, float pY) {
		super(sceneContext, null, pX, pY);
		this.sceneContext = sceneContext;
		this.width = RESERVATION_BOX_WIDTH;
		this.height = RESERVATION_BOX_HEIGHT;
		this.pX = pX;
		this.pY = pY;
		this.isGrabbed = false;
		this.parentReservation = null;

		VertexBufferObjectManager vertexBufferObjectManager = sceneContext.getEngine().getVertexBufferObjectManager();

		rectangle = new Rectangle(this.pX, this.pY, this.width, this.height, vertexBufferObjectManager);
		rectangle.setColor(0.35f, 0.35f, 0.35f);
		rectangle.setZIndex(10);

		this.mFont = FontFactory.create(sceneContext.getEngine().getFontManager(), sceneContext.getEngine().getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), 16, Color.WHITE);
		this.mFont.load();

		textName = new Text(2 + this.width / 2, this.height-10, this.mFont, "Name: ", new TextOptions(AutoWrap.LETTERS, this.width, HorizontalAlign.LEFT), vertexBufferObjectManager);
		rectangle.attachChild(textName);

        textNPeople = new Text(2 + this.width / 2, this.height-30 , this.mFont, " people", new TextOptions(AutoWrap.LETTERS, this.width, HorizontalAlign.LEFT), vertexBufferObjectManager);
        rectangle.attachChild(textNPeople);

		textDate = new Text(2 + this.width / 2, this.height-70, this.mFont, "Date: ", new TextOptions(AutoWrap.LETTERS, this.width, HorizontalAlign.LEFT), vertexBufferObjectManager);
		rectangle.attachChild(textDate);

		textHour = new Text(2 + this.width / 2, this.height-50, this.mFont, "Hour: ", new TextOptions(AutoWrap.LETTERS, this.width, HorizontalAlign.LEFT), vertexBufferObjectManager);
		rectangle.attachChild(textHour);
		//rectangleSceneB.attachChild(textHour);
		//rectangleSceneB.attachChild(textDate);
				
		sceneContext.getScene().attachChild(rectangle);

	}

// ===========================================================
// Getter & Setter
// ===========================================================

	public Shape getShape()
	{
		return (Shape) rectangle;
	}
	
	public float getX() {
		return rectangle.getX();
	}	

	public float getY() {
		return rectangle.getY();
	}
	
	@Override
	public void setX(float pX) {
		rectangle.setX(pX);
	}	
	
	@Override
	public void setY(float pY) {
		rectangle.setX(pY);
	}	

	public float getWidth() {
		return this.width;
	}

	public float getHeigth() {
		return this.height;
	}

	@Override
	public void setPosition(float pX, float pY)
	{
		this.rectangle.setPosition(pX, pY);
	}

	public void setScale(float pScale)
	{
		this.rectangle.setScale(pScale);
	}
	
	public void setVisible(Boolean visible)
	{
		this.rectangle.setVisible(visible);
	}
	
	public boolean getVisible()
	{
		return this.rectangle.isVisible();
	}
	
	public void setGrabbed(Boolean grabbed)
	{
		this.isGrabbed = grabbed;
	}
	
	public Boolean getGrabbed()
	{
		return this.isGrabbed;
	}
	
//	public Rectangle getSprite() {
//		return rectangleSceneB;
//	}
//	
	public void setBackgroundColor(float red, float green, float blue) {
		rectangle.setColor(red, green, blue);
	}


	public Reservation getParentReservation() {
		return parentReservation;
	}

	public void setParentReservation(Reservation parentReservation) {
		this.parentReservation = parentReservation;
	}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
	
	public void animate(SceneContext sceneContext, float startX, float startY,  float pX, float pY, float startZoomFactor, float endZoomFactor, int animationNumber)
	{
		boxDropper = new BoxDropper(sceneContext.getContext(), startX, startY, pX, pY, startZoomFactor, endZoomFactor, animationNumber);

		rectangle.registerEntityModifier(boxDropper.deepCopy());
	}

}